/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.styles {
	import mx.skins.ProgrammaticSkin;
	import flash.geom.Matrix;

	/**
	 *
	 * Use this to provide a nide background effect on any canvas.
	 *
	 * <listing>
	 * 	backgroundImage: ClassReference(”com.riaevolution.styles.GradientBackground”);
	 * 	backgroundSize: “100%”;
	 * 	fillColors: #EEEEEE, #999999;
	 * 	fillAlphas: 0.5, 0.5;
	 * </listing>
	 *
	 * <br /><b>Luciano bustos</b> <a href="mailto:luchyx&#64;riaevolution.com">luchyx&#64;riaevolution.com</a><br /><a href="http://www.riaevolution.com">www.riaevolution.com</a>
	 *
	 */
	public class GradientBackground extends ProgrammaticSkin {

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			var fillColors:Array = getStyle("fillColors");
			var fillAlphas:Array = getStyle("fillAlphas");
			var cornerRadius:int = getStyle("cornerRadius");
			var gradientType:String = getStyle("gradientType");
			var angle:Number = getStyle("angle");
			var focalPointRatio:Number = getStyle("focalPointRatio");

			// Default values, if styles aren't defined
			if (fillColors == null)
				fillColors = [0xEEEEEE, 0x999999];

			if (fillAlphas == null)
				fillAlphas = [1, 1];

			if (gradientType == "" || gradientType == null)
				gradientType = "linear";

			if (isNaN(angle))
				angle = 90;

			if (isNaN(focalPointRatio))
				focalPointRatio = 0.5;

			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(unscaledWidth, unscaledHeight, angle * Math.PI / 180);

			graphics.beginGradientFill(gradientType, fillColors, fillAlphas, [0, 255], matrix, "pad", "rgb", focalPointRatio);
			//graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			graphics.drawRoundRect(0, 0, unscaledWidth, unscaledHeight, cornerRadius * .5, cornerRadius * .5);
			graphics.endFill();
		}

		override public function get measuredWidth():Number {
			return parent ? parent.width : 100;
		}

		override public function get measuredHeight():Number {
			return parent ? parent.height : 100;
		}

	}

}